package com.exam.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.exam.business.UserManger;
import com.exam.domain.Administrator;
import com.exam.domain.Instructor;
import com.exam.domain.Student;
import com.mysql.jdbc.Driver;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {


	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String username=request.getParameter("textfield");
		String password=request.getParameter("textfield2");
		String type=request.getParameter("RadioGroup1");
		
		if(type.equalsIgnoreCase("admin"))
		{
			Administrator admin=UserManger.getInstance().findAdministratorById(username, password);
			if(!(admin==null))
			{
				request.getSession().setAttribute("admininfo", admin);
				
				response.sendRedirect(request.getContextPath()+"/admin.jsp");
				return;
			}
		}
		else if(type.equals("student"))
		{
			Student stu=UserManger.getInstance().findStudentById(username, password);
			if(!(stu==null))
			{
				request.getSession().setAttribute("stuinfo", stu);
				
				response.sendRedirect(request.getContextPath()+"/student.jsp");
				return;
			}
		}
		else if(type.equalsIgnoreCase("teacher"))
		{
			Instructor ins=UserManger.getInstance().findInstructorById(username, password);
			if(!(ins==null))
			{
				request.getSession().setAttribute("insinfo", ins);
				
				response.sendRedirect(request.getContextPath()+"/teacher.jsp");
				return;
			}
		}
		else
		{
			response.sendRedirect("LoginError.html");
			return;
		}
		response.sendRedirect("../LoginError.html");
		return;
	}
}
